package com.ship.boats.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.PageInfo;
import com.ship.boats.domain.dto.CreateOilinessPriceRq;
import com.ship.boats.domain.dto.ExportOilinessPriceRq;
import com.ship.boats.domain.dto.QueryOilinessPriceRq;
import com.ship.boats.domain.vo.ExportCurrencyExchangeRateInfo;
import com.ship.boats.domain.vo.ExportOilinessPriceInfo;
import com.ship.boats.domain.vo.QueryForwardOilinessInfo;
import com.ship.boats.domain.vo.QueryOilinessPriceInfo;
import com.ship.boats.service.OilinessPriceService;
import com.ship.boats.util.ExportDownloadUtil;
import com.ship.common.core.base.Page;
import com.ship.common.core.exception.ServiceException;
import com.ship.common.core.utils.poi.ExcelUtil;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.log.annotation.Log;
import com.ship.common.log.enums.BusinessType;
import com.ship.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <b>OilinessPriceController</b>
 *
 * @description: OilinessPriceController <br>
 * @date: 2024/3/19 09:12 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Api(tags = "油气价格指数模块")
@RestController
public class OilinessPriceController {
    private static final Logger logger = LoggerFactory.getLogger(OilinessPriceController.class);

    @Autowired
    private OilinessPriceService oilinessPriceService;

    @Autowired
    private ExportDownloadUtil exportDownloadUtil;

    @ApiOperation("新增油气价格指数信息")
    @PostMapping("/create/oiliness_price")
    public JsonResult createOilinessPrice(@RequestBody CreateOilinessPriceRq rq){
        logger.info("新增油气价格指数信息入参:{}"+ JSONUtil.parse(rq));
        boolean status = oilinessPriceService.createOilinessPrice(rq);
        return JsonResult.success();
    }


    @ApiOperation("分页查询油气指数价格列表")
    @PostMapping("/query/oiliness_price/list")
    public JsonResult<PageInfo<QueryOilinessPriceInfo>> queryOilinessPriceList(@RequestBody QueryOilinessPriceRq rq){
        logger.info("分页查询油气价格指数列表入参:{}"+JSONUtil.parse(rq));
        PageInfo<QueryOilinessPriceInfo> page = oilinessPriceService.queryPage(rq);
        return JsonResult.success(page);
    }

    @ApiOperation("分页查询远期油气价格指数列表")
    @PostMapping("/query/settle_oiliness_price/list")
    public JsonResult querySettleOilinessPriceList(@RequestBody QueryOilinessPriceRq rq){
        PageInfo<Object> pageInfo = oilinessPriceService.querySettleOilinessPriceList(rq);
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("油气价格导出")
    @PostMapping("/export/oiliness_price")
    public JsonResult exportOilinessPrice(HttpServletResponse response,
                                    @RequestBody ExportOilinessPriceRq rq){
        List<QueryForwardOilinessInfo> exportOilinessPriceInfos = oilinessPriceService.exportOilinessPrice(rq);
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("curveUID");
        tableNameList.add("curveDate");
        tableNameList.add("curveName");
        if ("S".equals(rq.getSettleFwd())){
            tableNameList.add("rangeFluctuation");
        }else if ("F".equals(rq.getSettleFwd())){
            tableNameList.add("rangeSeq");
            tableNameList.add("rangeBegDate");
            tableNameList.add("rangeEndDate");
        }
        tableNameList.add("periodicity");
        tableNameList.add("Value");
        tableNameList.add("oilinessUnit");
        tableNameList.add("dataSource");
        List<String> tableList = new ArrayList<>();
        tableList.add("curveUID");
        tableList.add("时间");
        tableList.add("指数名称");
        if ("S".equals(rq.getSettleFwd())){
            tableList.add("涨跌幅度");
        }else if ("F".equals(rq.getSettleFwd())){
            tableList.add("Range");
            tableList.add("周期开始时间");
            tableList.add("周期结束时间");
        }
        tableList.add("周期");
        tableList.add("价格");
        tableList.add("单位");
        tableList.add("数据来源");
        Field[] fields = QueryForwardOilinessInfo.class.getDeclaredFields();
        String url = exportDownloadUtil.exportUtil(exportOilinessPriceInfos,fields,tableNameList,tableList, "油气价格");
        return JsonResult.success(url);
    }


    @ApiOperation("查询油气指数名称列表")
    @Log(title = "油气价格",businessType= BusinessType.OTHER)
    @GetMapping("/query/oiliness_name/list")
    public JsonResult queryOilinessNameList(@RequestParam String settleFwd){
        List<String> nameList = oilinessPriceService.queryOilinessNameList(settleFwd);
        return JsonResult.success(nameList);
    }
}
